<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Browsing devices and services</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="gupnp.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="gupnp.binary-light.html">Implementing the BinaryLight device</a></div>
 <div class="up"><a href="gupnp.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="gupnp.browsing" class="section">
  <h2 class="title">Browsing devices and services</h2>
  <p class="para">
   This example shows how to obtain information about all devices and
   services.  It starts an infinite loop (use CLI), and if any of
   available devices or services are found, the proper callback
   function will be invoked.
  </p>
  <p class="para">
   <div class="example" id="example-4244">
    <p><strong>Example #1 Search for all UPnP devices and services.</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">/*&nbsp;Callback&nbsp;for&nbsp;available&nbsp;device&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">device_proxy_available_cb</span><span style="color: #007700">(</span><span style="color: #0000BB">$proxy</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arg</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gupnp_device_info_get</span><span style="color: #007700">(</span><span style="color: #0000BB">$proxy</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$type&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">'device_type'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$location&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">'location'</span><span style="color: #007700">];<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Device&nbsp;available:\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"\ttype:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$type</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"\tlocation:&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Callback&nbsp;for&nbsp;available&nbsp;service&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">service_proxy_available_cb</span><span style="color: #007700">(</span><span style="color: #0000BB">$proxy</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arg</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$info&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gupnp_service_info_get</span><span style="color: #007700">(</span><span style="color: #0000BB">$proxy</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$type&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">'service_type'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$location&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">'location'</span><span style="color: #007700">];<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Service&nbsp;available:\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"\ttype:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$type</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"\tlocation:&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Create&nbsp;the&nbsp;UPnP&nbsp;context&nbsp;*/<br /></span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gupnp_context_new</span><span style="color: #007700">();<br />if&nbsp;(!</span><span style="color: #0000BB">$context</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Error&nbsp;creating&nbsp;the&nbsp;GUPnP&nbsp;context\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;We're&nbsp;interested&nbsp;in&nbsp;everything&nbsp;*/<br /></span><span style="color: #0000BB">$cp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gupnp_control_point_new</span><span style="color: #007700">(</span><span style="color: #0000BB">$context</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"ssdp:all"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Set&nbsp;callbacks&nbsp;*/<br /></span><span style="color: #0000BB">gupnp_control_point_callback_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$cp</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">GUPNP_SIGNAL_DEVICE_PROXY_AVAILABLE</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'device_proxy_available_cb'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">gupnp_control_point_callback_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$cp</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">GUPNP_SIGNAL_SERVICE_PROXY_AVAILABLE</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'service_proxy_available_cb'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Start&nbsp;for&nbsp;browsing&nbsp;(infinite&nbsp;loop,&nbsp;hit&nbsp;Ctrl-C&nbsp;to&nbsp;interrupt)&nbsp;*/<br /></span><span style="color: #0000BB">gupnp_control_point_browse_start</span><span style="color: #007700">(</span><span style="color: #0000BB">$cp</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="gupnp.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="gupnp.binary-light.html">Implementing the BinaryLight device</a></div>
 <div class="up"><a href="gupnp.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
